import { login } from '@/api/login'
import { setToken, getToken, removeToken, setProjectState, getProjectState } from '@/utils/auth'
import { setPermission, getPermission, getLocalPermission, setLocalPermission } from '@/utils/permission'
// import { getLocalUid, setLocalUid } from '@/utils/uid'
import { getLocalUserName, setLocalUserName } from '@/utils/uid'

const user = {
  state: {
    token: getToken(),
    projectState: getProjectState(),
    name: '',
    avatar: '',
    roles: [],
    button: []
  },

  mutations: {
    SET_BUTTON: (state, buttons) => {
      state.button = buttons
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_PROJECT_STATE: (state, projectState) => {
      state.projectState = projectState
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    }
  },

  actions: {
    // 登录
    Login({ commit }, formdata) {
      return new Promise((resolve, reject) => {
        // 不加登录代码 测试用
        // console.log('登录:', formdata)
        // if (formdata.username === 'admin' && formdata.password === '123456' || formdata.username === 'sale' && formdata.password === '123456' || formdata.username === 'casher' && formdata.password === '123456') {
        //   setLocalUserName(formdata.username) // 记录用户名
        //   commit('SET_TOKEN', 'token_128127122912102')
        //   setToken('token_128127122912102')
        //   resolve('登录')
        // } else {
        //   if (formdata.username === 'admin' || formdata.username === 'sale' || formdata.username === 'casher') {
        //     reject({ message: '密码错误' })
        //   }
        //   if (formdata.password === '123456') {
        //     reject({ message: '用户名不存在' })
        //   }
        // }

        // 加登录代码 正式用
        login(formdata).then(response => {
          if (response.code === '200') {
            console.log('登陆数据：', response)
            console.log(JSON.stringify(response.data[0]))
            setLocalPermission(JSON.stringify(response.data))
            // setLocalUid(response.data[0].uid)
            // console.log('getLocalPermission:', getLocalPermission())
            commit('SET_TOKEN', 'token_128127122912102')
            setToken('token_128127122912102')
            resolve(response)
          } else {
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit }) {
      return new Promise((resolve) => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        removeToken()
        resolve()
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },

    getUserBtn({ commit }, state) {
      console.log('vuexState:', state)
      commit('SET_BUTTON', state)
    }
  }
}

export default user
